﻿using System;
using System.Data;
using DataAccess;

namespace Business
{
    public class BuChart
    {
        public static string selChartAcc(int activityProjectID, string companyid)
        {
            string result = String.Empty;
            DaChart daChart = new DaChart();
            DataTable tmpChart = daChart.selChartAcc(activityProjectID, companyid);

            int numOfRow = tmpChart.Rows.Count;
            if (numOfRow > 0)
            {
                result += "http://chart.apis.google.com/chart?chxp=1,0,5,10,15&chxs=0,FF9900,11.5,0,l,676767|1,224499,12.5,0.5,l,676767&chxt=y,x&chs=694x250&cht=lc&chco=3D7930&chg=14.3,-1,1,1&chls=2,4,0&chm=B,C5D4B5BB,0,0,0";
                // Tinh so ngay dang ky
                DateTime start = DateTime.Parse(tmpChart.Rows[0]["Date"].ToString());
                DateTime end = DateTime.Parse(tmpChart.Rows[numOfRow-1]["Date"].ToString());
                TimeSpan timeSpan = end - start;
                int numOfDay = timeSpan.Days;
                int dayAdd = 0;
                if(numOfDay % 4 == 0)
                {
                    dayAdd = numOfDay / 4;
                }else{
                    dayAdd = numOfDay / 4 + 1;
                }
                result += "&chxl=1:|" + start.ToString("dd/MM/yyyy") + "|" + start.AddDays(dayAdd).ToString("dd/MM/yyyy") + "|" + start.AddDays(dayAdd * 2).ToString("dd/MM/yyyy") + "|" + end.ToString("dd/MM/yyyy");
                // Duyet cac hang cua datatable
                result += "&chd=t:";
                int j = 0;
                double value = 0;
                for (int i = 0; i < numOfDay; i++)
                {
                    if (start.AddDays(i).Equals(tmpChart.Rows[j]["Date"]))
                    {
                        value += Double.Parse(tmpChart.Rows[j]["SumHour"].ToString()) + Double.Parse(tmpChart.Rows[j]["SumMinute"].ToString()) / 60;
                        j++;
                    }
                    if (i != numOfDay - 1)
                    {
                        result += String.Format("{0:0.0}", value)+",";
                    }
                    else
                    {
                        result += String.Format("{0:0.0}", value);
                    }
                }

                result += "&chxr=0,0,"+(int)(value+1)+"|1,0,15";
                result += "&chds=0," + (int)(value + 1);
            }
            
            return result;
        }

        public static string selChartAccProject(int activityProjectID, string companyid)
        {
            string result = String.Empty;
            DaChart daChart = new DaChart();
            DataTable tmpChart = daChart.selChartAcc(activityProjectID, companyid);

            int numOfRow = tmpChart.Rows.Count;
            if (numOfRow > 0)
            {
                result += "http://chart.apis.google.com/chart?chxp=1,0,15&chxs=0,FF9900,11.5,0,l,676767|1,224499,12.5,0.5,l,676767&chxt=y,x&chs=425x125&cht=lc&chco=3D7930&chg=14.3,-1,1,1&chls=2,4,0&chm=B,C5D4B5BB,0,0,0";
                // Tinh so ngay dang ky
                DateTime start = DateTime.Parse(tmpChart.Rows[0]["Date"].ToString());
                DateTime end = DateTime.Parse(tmpChart.Rows[numOfRow - 1]["Date"].ToString());
                TimeSpan timeSpan = end - start;
                int numOfDay = timeSpan.Days;
                result += "&chxl=1:|" + start.ToString("dd/MM/yyyy") +"|" + end.ToString("dd/MM/yyyy");
                // Duyet cac hang cua datatable
                result += "&chd=t:";
                int j = 0;
                double value = 0;
                for (int i = 0; i <= numOfDay; i++)
                {
                    if (start.AddDays(i).Equals(tmpChart.Rows[j]["Date"]))
                    {
                        value += Double.Parse(tmpChart.Rows[j]["SumHour"].ToString()) + Double.Parse(tmpChart.Rows[j]["SumMinute"].ToString()) / 60;
                        j++;
                    }
                    if (i != numOfDay)
                    {
                        result += String.Format("{0:0.0}", value) + ",";
                    }
                    else
                    {
                        result += String.Format("{0:0.0}", value);
                    }
                }
                //string tmp = start.AddDays(numOfDay - 1).ToString();
                result += "&chxr=0,0," + (int)(value + 1) + "|1,0,15";
                result += "&chds=0," + (int)(value + 1) + "&chtt=Thời+gian+cộng+dồn";
            }

            return result;
        }
    }
}
